/**
 * Created by bestick on 2015/12/27.
 * 信托详情页函数库
 */
$(document).ready(function () {
    /* =================================================== trustDetail =================================================== */

    //默认参数
    var trustDetailURL = "/api/client/tnonstandardproduct/showtnonstandardproduct";

    var pptURL = "/api/util/newfile/getppt";

    var commentURL = "/api/client/comment/showproductcomment";

    var addCommentURL = "/api/client/comment/addproductcomment";

    var concernURL = "/api/client/concerned/addconcerned";

    var nowPage = 1;

    var count = 3;

    var trustDetailMap = {
        "#trustDetail-info-temp": "#trustDetail-info",
        "#trustDetail-desc-temp": "#trustDetail-desc",
        "#trustDetail-exhibition-temp": "#trustDetail-exhibition",
        "#trustDetail-ppt-footer-temp": "#trustDetail-ppt-footer",
        "#jcb-financial-calculator-temp": "#jcb-financial-calculator"
    };

    var pptMap = {"#trustDetail-ppt-temp": "#trustDetail-ppt"};

    var commentMap = {"#detail-evaluate-content-temp": "#detail-evaluate-content"};

    var jsonData = '';

    //渲染之后需要加载
    function initTrustDetail() {
        initSlider();
        initBtnStyle();
        initJCBCalculator();
        initJcbCalChart();
    }

    $.ajax({
        type: "GET",
        async: false,
        url: trustDetailURL,
        data: {
            "productId": $('.trustDetail').data("pid"),
            "t": now()
        },
        dataType: "json",
        success: function (data) {
            jsonData = data;
        }
    });

    function initBtnStyle() {
        $('.btn-jcb-red').focus(function () {
            this.blur();
        });
        $('#concerned').click(function () {
            doPost(
                concernURL,
                {
                    "productId": $('.trustDetail').data("pid"),
                    "t": now()
                }
            );
        });
    }

    function initSlider() {
        $('input.slider').slider();
    }

    function initSwiper() {
        new Swiper('.swiper-container', {
            pagination: '.swiper-pagination',
            paginationClickable: '.swiper-pagination',
            nextButton: '.swiper-button-next',
            prevButton: '.swiper-button-prev',
            spaceBetween: 30
        });
    }

    doRender(
        trustDetailURL,
        {
            "productId": jsonData.data.pId,
            "t": now()
        },
        trustDetailMap,
        initTrustDetail
    );

    doRender(
        pptURL,
        {
            "filepath": jsonData.data.filePosition,
            "t": now()
        },
        pptMap,
        initSwiper
    );

    //评论渲染
    function commentRender(callback, nowPage, count, url, pid, map) {
        doRender(
            url,
            {
                "commentBelongToProduct": pid,
                "start": (nowPage - 1) * count,
                "count": count,
                "t": now()
            },
            map,
            function (XMLHttpRequest) {
                //获取total
                var total = eval('(' + XMLHttpRequest.responseText + ')').total;
                if (total >= 0) {
                    //在目标节点后append分页组件
                    $('#detail-evaluate-content').append(genPagination(total, count, nowPage));
                    initPagination(callback, nowPage, count, url, pid, map);
                }
            }
        )
    }

    //执行评论渲染
    commentRender(commentRender, nowPage, count, commentURL, jsonData.data.pId, commentMap);

    //提交评论
    $('#send-evaluate').click(function () {
        var commentContent = $('#my-comment').val();
        if (commentContent == '') {
            $(this).prev().html('亲，请您填写评论后再提交哦！'). fadeIn();
            return;
        }
        doPost(
            addCommentURL,
            {
                "productId": jsonData.data.pId,
                "commentContent": commentContent
            },
            function () {
                $('#myEvaluate').modal('hide').find('#error-msg').fadeOut();
                commentRender(commentRender, nowPage, count, commentURL, jsonData.data.pId, commentMap);
            }
        );
    });
});